/**
 * 修改个人资料
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/29
 */
var self = memberInfo = nickname = signature = email = avatar = '';
var index = 1;
var files = []; 
document.addEventListener('plusready' , ready , false);
function ready(){
	self = plus.webview.currentWebview();
//	plus.storage.removeItem('memberInfo')
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo); 
 	if(memberInfo.avatar != null)avatar = memberInfo.avatar;
	$('.home_top img').attr('src' , getImgUrl(avatar))
	$('.mui-table-view input').each(function(index){
		switch (index){
			case 0:
				if(memberInfo.nickname) $(this).attr('value' , memberInfo.nickname);
				break;
			case 1:
				if(memberInfo.signature) $(this).attr('value' , memberInfo.signature);
				break;
			case 2:
				$(this).attr('placeholder' , memberInfo.phone);
				break;
			case 3:
				if(memberInfo.email) $(this).attr('value' , memberInfo.email);
				break;
		}
	})
	 
	
	//修改用户头像
 	$('.home_top').on('click' , function(){
 		plus.nativeUI.actionSheet( {cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]}, function(e){
			if(e.index==1){
				var cmr = plus.camera.getCamera();
				cmr.captureImage(function(path) {
					$('.home_top img').attr('src','file://'+plus.io.convertLocalFileSystemURL(path));
					appendPic(path);
				}, function(err) {});
				
			}else if(e.index==2){
				plus.gallery.pick(function(path){
					$('.home_top img').attr('src',path);
					appendPic(path);
			    });
			}
		});
 	})
	
	
	
	//保存用户信息
	$('.save').on('click' , function(){
		$('.mui-table-view input').each(function(index){
			switch (index){
				case 0:
					nickname = $(this).val();
					break;
				case 1:
					signature = $(this).val();
					break;
				case 2:
					phone = $(this).attr('placeholder');
					break;
				case 3:
					email = $(this).val();
					break;
			}
		})
		
		if(signature.length > 15){
			toast('签名长度不能超过15个字符');
			return;
		}
		
		if(!p3.test(email)){
			toast('邮箱格式有误'); return;
		}
		
//		console.log(apiRoot + '/Home/Member/updateInfo/aid/'+memberInfo.aid+'/avatar/' +avatar+ '/nickname/' + nickname + '/signature/' +signature + '/phone/' + phone +'/email/' + email);
//		return;
		
		$.ajax({
			url : apiRoot + '/Home/Member/updateInfo/' , 
			type : 'post' , 
			data : {
				aid : memberInfo.aid , 
				avatar : avatar , 
				nickname : nickname ,
				signature : signature , 
				phone : phone , 
				email : email ,
				money : memberInfo.money 
			} , 
			beforeSend:function(){
				plus.nativeUI.showWaiting('保存中...');
			},
			beforeSend:function(){
				plus.nativeUI.closeWaiting();
			},
			success : function(data){
				var result = eval('('+data+')');
				if(result){
					toast('用户数据已更新');
					//保存用户数据
					plus.storage.setItem('memberInfo' , data + '')
					mui.fire(plus.webview.getWebviewById(self.page) , 'update_personal_info' ,{
						avatar : avatar , 
						nickname : nickname,
						signature : signature
					})
					setTimeout(function(){
						self.close();
					} , 2000)
				}
			} , 
			error : function(e){
				errorPrompt(e);
			}
		})
		
	})
	
   	
	
// 	
// 	
// 	
	
	
	
	
//	mui.fire(plus.webview.getWebviewById(self.page) , 'update_personal-info' , {
//		aid : 541
//	})
	
}
// 添加照片
function appendPic(p) {  
	var newPath = p.replace(/\./g , new Date().getTime()+'.');
	plus.zip.compressImage(
		{
			src:p,
			dst:newPath,
			quality:20
		},
		function() {
			files.push({name:"uploadkey"+index,path:p});
			index++;
			upload();
		},function(error) {
			toast('压缩图片失败');
		}
	);
}

function upload(){
	plus.nativeUI.showWaiting('图片处理中...');
	if(files.length <= 0){  
		toast("请上传头像！");
		return;
	}
	var server = apiRoot+'/Home/Member/uploadPic/'; 
	var task = plus.uploader.createUpload(server,
		{method:"POST"},
		function(t,status){ //上传完成
			plus.nativeUI.closeWaiting();
			if(status == 200){
				toast('处理完成');
				avatar = t.responseText;
			}else{
				plus.nativeUI.alert("上传失败："+status);
				plus.nativeUI.closeWaiting();
			}
		} 
	);
	for(var i = 0;i < files.length ; i++){
		var f = files[i];
		task.addFile(f.path,{key:f.name});
	} 
	task.start();
}

